package com.cskaoyan.service;


import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.beanwx.vo.SearchIndexVo;
import com.cskaoyan.dao.KeywordMapper;
import com.cskaoyan.dao.SearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author: lds
 * @clsss: SearchServiceImpl
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    /**
     * @author: lds
     * @title: queryKeyWords
     * @description: 根据ishot,isdefault分类查询keyword,根据userid查询history keyword
     * @param: [user]
     * @return: com.cskaoyan.beanwx.vo.SearchIndexVo
     */
    @Override
    public SearchIndexVo queryKeyWords(User user) {
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();


        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria1 = searchHistoryExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria1.andUserIdEqualTo(user.getId());

        List<SearchHistory> searchHistories = null;

        List<Keyword> keywords = null;

        try {
            searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);

            keywords = keywordMapper.selectByExample(keywordExample);
        } catch (Exception e) {
            return null;
        }


        LinkedList<String> historyKeywords = new LinkedList<>();

        for (SearchHistory searchHistory : searchHistories) {
            if (!historyKeywords.contains(searchHistory.getKeyword())) {
                historyKeywords.add(searchHistory.getKeyword());
            }
        }


        Keyword defaultKeyword = null;
        LinkedList<Keyword> hotKeywordList = new LinkedList<>();
        LinkedList<Keyword> historyKeywordList = new LinkedList<>();
        for (Keyword keyword : keywords) {
            if (keyword.getIsDefault() && !keyword.getDeleted()) {
                defaultKeyword = keyword;
            }
            if (keyword.getIsHot() && !keyword.getDeleted()) {
                hotKeywordList.add(keyword);
            }

            if (historyKeywords.contains(keyword.getKeyword())) {
                historyKeywordList.add(keyword);
            }
        }

        SearchIndexVo searchIndexVo = new SearchIndexVo();
        searchIndexVo.setDefaultKeyword(defaultKeyword);
        searchIndexVo.setHotKeywordList(hotKeywordList);
        searchIndexVo.setHistoryKeywordList(historyKeywordList);


        return searchIndexVo;
    }

    @Override
    public Integer removeHistory(User user) {

        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria1 = searchHistoryExample.createCriteria();
        criteria1.andUserIdEqualTo(user.getId());
        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setDeleted(true);
        Integer i=null;
        try {
            i = searchHistoryMapper.updateByExampleSelective(searchHistory, searchHistoryExample);
        }catch (Exception e) {
            return 404;
        }

            return i;
    }

    @Override
    public List queryHelperKeyWords(String keyword) {

        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andKeywordLike("%" + keyword + "%");
        criteria.andDeletedEqualTo(false);
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

        LinkedList<String> keywordList = new LinkedList<>();

        for (Keyword keyword1 : keywords) {
            keywordList.add(keyword1.getKeyword());
        }
        return keywordList;
    }
}
